package org.example.ims.Controller.Order;

import org.example.ims.DTO.Response;
import org.example.ims.Entity.Order.InStockOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.example.ims.Service.Order.InStockOrderService;


@RestController
@RequestMapping("/inStockOrder")
public class InStockOrderController {

    @Autowired
    private  InStockOrderService inStockOrderService;

    @PostMapping
    public ResponseEntity<InStockOrder> createOrder(@RequestBody InStockOrder order) {
        InStockOrder createdOrder = inStockOrderService.createOrder(order);
        return ResponseEntity.ok(createdOrder);
    }

    @PostMapping("/{orderId}/execute")
    public ResponseEntity<InStockOrder> executeOrder(@PathVariable Integer orderId ) {
        inStockOrderService.executeOrder(orderId);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/{orderId}")
    public ResponseEntity<InStockOrder> updateOrder(@PathVariable Integer orderId, @RequestBody InStockOrder order) {
        InStockOrder updatedOrder = inStockOrderService.updateOrder(orderId, order);
        return ResponseEntity.ok(updatedOrder);
    }

    @DeleteMapping("/{orderId}")
    public ResponseEntity<Void> deleteOrder(@PathVariable Integer orderId) {
        inStockOrderService.deleteOrder(orderId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<InStockOrder> getOrderById(@PathVariable Integer orderId) {
        InStockOrder order = inStockOrderService.getOrderById(orderId);
        return ResponseEntity.ok(order);
    }

    @GetMapping("/search")
    public ResponseEntity<Page<InStockOrder>> getAllOrders(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<InStockOrder> orders = inStockOrderService.getAllOrders(page, size);
        return ResponseEntity.ok(orders);
    }

    @GetMapping
    public ResponseEntity<Response<Page<InStockOrder>>> searchOrders(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer supplierId,
            @RequestParam(required = false) Integer salesmanId,
            @RequestParam(required = false) Integer warehouseId,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<InStockOrder> orders = inStockOrderService.searchOrders(status, supplierId, salesmanId, warehouseId,page, size);
        return ResponseEntity.ok(Response.success(orders));
    }
}